package com.xiaomi.controller;

import com.xiaomi.constant.MessageConstant;
import com.xiaomi.entity.Rule;
import com.xiaomi.result.Result;
import com.xiaomi.service.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rule")
@Slf4j
public class RuleController {
    @Autowired
    private RuleService ruleService;

    /**
     * 添加规则
     * @param rule
     * @return
     */
    public Result addRule(@RequestBody Rule rule){
        log.info("添加规则：{}", rule);
        ruleService.addRule(rule);
        return Result.success(MessageConstant.ADD_RULE_SUCCESS);
    }
    /**
     * 删除规则
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteRule(@PathVariable Long id){
        log.info("删除规则，ID：{}", id);
        ruleService.deleteRule(id);
        return Result.success(MessageConstant.DELETE_RULE_SUCCESS);
    }

    /**
     * 更新规则
     * @param rule
     * @return
     */
    @PutMapping("/update")
    public Result updateRule(@RequestBody Rule rule){
        log.info("更新规则：{}", rule);
        ruleService.updateRule(rule);
        return Result.success(MessageConstant.UPDATE_RULE_SUCCESS);
    }

    /**
     * 根据ID查询规则
     * @param id
     * @return
     */

    @GetMapping("/get/{id}")
    public Result<Rule> getRuleById(@PathVariable Long id){
        log.info("查询规则，ID：{}", id);
        Rule rule = ruleService.getRuleById(id);
        return Result.success(MessageConstant.QUERY_RULE_SUCCESS, rule);
    }

    /**
     * 查询所有规则
     * @return
     */
    @GetMapping("/getAll")
    public Result<List<Rule>> getAllRules(){
        log.info("查询所有规则");
        List<Rule> rules = ruleService.getAllRules();
        return Result.success(MessageConstant.QUERY_ALL_RULES_SUCCESS, rules);
    }

    /**
     * 根据电池类型查询规则
     * @param batteryType
     * @return
     */
    @GetMapping("/getByBatteryType/{batteryType}")
    public Result<List<Rule>> getRulesByBatteryType(@PathVariable String batteryType){
        log.info("根据电池类型查询规则，电池类型：{}", batteryType);
        List<Rule> rules = ruleService.getRulesByBatteryType(batteryType);
        return Result.success(MessageConstant.QUERY_RULES_BY_BATTERY_TYPE_SUCCESS, rules);
    }
}
